﻿// <copyright file="AppSettingsViewModel.cs" company="EffuEfface">
//      Copyright (c) Effu Efface Consulting  All rights reserved.
// </copyright>
// <author>Ryan R. Sites</author>

namespace TVersiPlay.AppSettings.ViewModels
{
    using System.Collections.Generic;
    using System.Collections.ObjectModel;
    using System.ComponentModel;
    using System.Windows.Input;
    using Microsoft.Practices.Prism.Commands;
    using Microsoft.Practices.Prism.Events;
    using TVersiPlay.Common.Interfaces;
    using TVersiPlay.DTO;
    using TVersiPlay.Events;

    public class AppSettingsViewModel: IAppSettingsViewModel
    {
        #region declarations

        private IUPnPControlPointService upnpService;
        private IAppSettingsService appSettings;

        private IEventAggregator eventAggregator;

        private ICommand serverSelected;

        private int selectedServerIndex;

        private ObservableCollection<UPnPServer> availableServers;

        #endregion //declarations

        #region constructors

        public AppSettingsViewModel(IUPnPControlPointService upnpService, IAppSettingsService appSettings, IEventAggregator eventAggregator)
        {
            this.upnpService = upnpService;
            this.appSettings = appSettings;
            this.eventAggregator = eventAggregator;

            this.serverSelected = new DelegateCommand<UPnPServer>(this.OnServerSelected);

            this.eventAggregator.GetEvent<UPnPServerRefreshedEvent>().Subscribe(this.RefreshServers,ThreadOption.UIThread);
            this.InitializeSettings();
        }

        #endregion //constructors

        #region properties

        public ICommand ServerSelected
        {
            get { return this.serverSelected; }
        }

        public ObservableCollection<UPnPServer> AvailableServers
        {
            get
            {
                return this.availableServers;
            }
            set
            {
                this.availableServers = value;
            }
        }

        public int SelectedServerIndex
        {
            get
            {
                return this.selectedServerIndex;
            }
            set
            {
                this.selectedServerIndex = value;
                this.OnPropertyChanged("SelectedServerIndex");
            }
        }
        #endregion //properties

        

        #region methods

        private void RefreshServers(object paramater)
        {
            this.InitializeSettings();
            this.OnPropertyChanged("AvailableServers");
        }

        private void InitializeSettings()
        {
            List<UPnPServer> temp = this.upnpService.GetAvailableServers();
            
            this.AvailableServers = new ObservableCollection<UPnPServer>();
            foreach (var e in temp)
            {
                this.AvailableServers.Add(e);
            }
        }

        #endregion //methods

        void OnServerSelected(UPnPServer selectedServer)
        {
            this.upnpService.SetCurrentTVersityServer(selectedServer.Server);
        }

        #region events

        

        #endregion //events

        #region INotifyPropertyChanged Items

        public event PropertyChangedEventHandler PropertyChanged;

        private void OnPropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler Handler = PropertyChanged;
            if (Handler != null)
                Handler(this, new PropertyChangedEventArgs(propertyName));
        }

        #endregion

        
    }
}
